package org.mozhu.zufang.system.auth.service;

import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * AuthorityService的聚合类，聚合Spring容器中所有AuthorityService的查询结果，达到可插拔的效果
 * DefaultAuthorityService - 返回所有默认的Role,Permission
 * UserAuthAuthorityService - 返回某个User的Role,Permission
 * UserGroupAuthAuthorityService - 返回某个User所在Group的Role,Permission
 * 将来可以根据需求添加组织机构界别的权限，只需将bean配置在Spring容器中即可
 */
@Service("authorityService")
public class AuthorityServiceProxy implements AuthorityService {

    private List<AuthorityService> delegates;

    @Autowired
    public AuthorityServiceProxy(List<AuthorityService> delegates) {
        this.delegates = delegates;
    }

    @Override
    public Set<String> findStringRoles(Long userId) {
        Set<String> roles = Sets.newHashSet();
        for (AuthorityService delegate : delegates) {
            roles.addAll(delegate.findStringRoles(userId));
        }
        return roles;
    }

    @Override
    public Set<String> findStringPermissions(Long userId) {
        Set<String> permissions = Sets.newHashSet();
        for (AuthorityService delegate : delegates) {
            permissions.addAll(delegate.findStringPermissions(userId));
        }
        return permissions;
    }

}
